package com.k.liuzh.extraffic;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.k.liuzh.extraffic.Fragment.HLDeng_Fragment;
import com.k.liuzh.extraffic.Fragment.HuanJing_Fragment;
import com.k.liuzh.extraffic.Fragment.TestFargment;
import com.k.liuzh.extraffic.Fragment.WeiZhang_Fragment;
import com.k.liuzh.extraffic.Fragment.ZhangDan_Fragment;
import com.k.liuzh.extraffic.Fragment.ZhangHu_Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private SlidingPaneLayout slidingPaneLayout;
    private ListView listView;
    private FragmentManager fragmentManager;
    private TextView topTextView;

    private TestFargment testFargment;
    private List<Map<String,Object>> dataList;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initView() {
        FragmentTransaction transaction;
        imageView = findViewById(R.id.main_img);
        slidingPaneLayout = findViewById(R.id.main_layout);
        listView = findViewById(R.id.main_list);
        topTextView = findViewById(R.id.main_topText);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        testFargment = new TestFargment();

        transaction.add(R.id.main_fragment,testFargment);
        transaction.commit();

    }

    private void initData() {

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(slidingPaneLayout.isOpen()){
                    closeSlidingPane();
                }else {
                    openSlidingPane();
                }
            }
        });
        String[] name = {"车辆违章","我的账户","红绿灯管理","账单管理","环境指标"};
        dataList = new ArrayList<>();
        for(String a : name){
            Map<String,Object> map = new HashMap<>();
            map.put("name",a);
            dataList.add(map);
        }
        listView.setAdapter(new MyAdpter(dataList,getApplicationContext()));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this,"this is int i "+i+"  this i id"+l,Toast.LENGTH_SHORT).show();
                switch (i){
                    case 0:
                        replaceFragment(new WeiZhang_Fragment());
                        topTextView.setText("车辆违章");
                        break;
                    case 1:
                        replaceFragment(new ZhangHu_Fragment());
                        topTextView.setText("我的账户");
                        break;
                    case 2:
                        replaceFragment(new HLDeng_Fragment());
                        topTextView.setText("红绿灯管理");
                        break;
                    case 3:
                        replaceFragment(new ZhangDan_Fragment());
                        topTextView.setText("账单管理");
                        break;
                    case 4:
                        replaceFragment(new HuanJing_Fragment());
                        topTextView.setText("环境指标");
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void closeSlidingPane(){
        slidingPaneLayout.closePane();
    }
    public void openSlidingPane(){
        slidingPaneLayout.openPane();
    }

    public void replaceFragment(Fragment fragment){
        Bundle bundle = new Bundle();
        Intent intent = getIntent();
        String userName = intent.getStringExtra("userName");
        bundle.putString("userName",userName);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        fragment.setArguments(bundle);
        transaction.replace(R.id.main_fragment,fragment);
        transaction.commit();
        closeSlidingPane();
    }
    private class MyAdpter extends BaseAdapter{

        private List<Map<String,Object>> mData;
        private LayoutInflater mInflater;
        private Context context;

        public MyAdpter(List<Map<String,Object>> mData,Context context) {
            this.mData = mData;
            this.mInflater = LayoutInflater.from(context);
            this.context = context;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int i) {
            return dataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater inflater = LayoutInflater.from(context);
            ViewHolder viewHolder = null;
            if(viewHolder == null){
                viewHolder = new ViewHolder();
                view = inflater.inflate(R.layout.menu_layout,null);
                viewHolder.textView = view.findViewById(R.id.list_text);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) view.getTag();
            }
            Map<String,Object> map = dataList.get(i);
            viewHolder.textView.setText((String)map.get("name"));
            return view;
        }
        class ViewHolder{
            TextView textView;
        }
    }

}
